import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.Stack;

/**
 *  120 - Stacks of Flapjacks - Uva - Online-judge.org
 *  Solved in JAVA by Jonas Jeske
 *
 * @author Jonas Jeske
 * @see http://www.inf.ufrgs.br/~jjeske
 *
 *	Status: Accepted !
 */
public class Main {

	public static void main(String[] args){
		Main stackOfFlapjacks = new Main();
		stackOfFlapjacks.begin();		
	}
	
	public PrintStream saida = new PrintStream(System.out);
	
	/*Sample Input

	1 2 3 4 5
	5 4 3 2 1
	5 1 2 3 4
	
	Sample Output
	
	1 2 3 4 5
	0
	5 4 3 2 1
	1 0
	5 1 2 3 4
	1 2 0
	*/
	void begin(){
		Scanner entrada = new Scanner(System.in);
		int tokenType;
		String stringAux;		
		Stack<Integer> pilha = new Stack<Integer>();
		ArrayList<Integer> lista = new ArrayList<Integer>();
		
		entrada.useDelimiter(System.getProperty("line.separator"));		
		while (entrada.hasNext()) {
			stringAux = entrada.next();
			if (stringAux.isEmpty()){
				break; 
			}
			Scanner entradaLinha = new Scanner(stringAux);
			while (entradaLinha.hasNextInt()) {
				lista.add(Integer.valueOf(entradaLinha.nextInt()));
			}
			
			Collections.reverse(lista);
			for(int i=0; i < lista.size(); i++){
				pilha.push(lista.get(i));
			}
			
			Collections.sort(lista);		
			searchListOrderStack(lista,pilha);

			pilha.removeAllElements();
			lista.removeAll(lista);
			saida.printf("0\n");
		}		
	}
	
	/**
	 * Searches the maximum integer element of the Stack
	 * Make flips on the stack according to the max element
	 * Stop when the stack is ordered.
	 * !! O ultimo elemento da Lista eh o maior!!
	 * 
	 * @param lista
	 * @param pilha
	 */
	private void searchListOrderStack(ArrayList<Integer> lista,
			Stack<Integer> pilha) {		
		int stackSize = pilha.size()-1; 
		int elementPosition = 0;
		int maxElementOnStack = 0;
		
//		System.out.println("---Original not Fliped---");
//		System.out.println("Pilha: " + pilha.toString());				
//		System.out.println("---");
		
		printStack(pilha);		
		while(stackSize > 0){
			
			maxElementOnStack = Integer.valueOf(lista.get(stackSize));			
			elementPosition = findMaxElementReturnPosition(pilha,stackSize,maxElementOnStack);
			
//			System.out.println("Pilha: " + pilha.toString());
//			System.out.println("stackSize " + stackSize);
//			System.out.println("maxElementOnStack: " + maxElementOnStack);		
//			System.out.println("Lista: " + lista.toString());			
//			System.out.println("element Position on Stack: " + elementPosition);
//			System.out.println("---");
			if(elementPosition == 0){
				flip(pilha,stackSize,stackSize);
//				System.out.println("Pilha fliped: " + pilha.toString());
			}
			if ((elementPosition != stackSize)&&(elementPosition != 0)) {
				flip(pilha, stackSize, elementPosition);
//				System.out.println("Pilha fliped: " + pilha.toString());
				flip(pilha, stackSize, stackSize);
//				System.out.println("Pilha fliped: " + pilha.toString());
			}
			
			stackSize--;
		}				
	}

	private void printStack(Stack<Integer> pilha) {
		Stack<Integer> copia = (Stack<Integer>) pilha.clone();
		for(int position = 0;position < pilha.size()-1; position++){
			saida.printf("%d ",copia.pop().intValue());
		}
		saida.printf("%d",copia.pop().intValue());
		saida.printf("\n");		
	}

	/**
	 * Flips the sub-stack with element Position
	 * @param pilha
	 * @param stackSize
	 * @param elementPosition
	 */
	private void flip(Stack<Integer> pilha, int stackSize, int elementPosition) {
		ArrayList<Integer> bufferList = new ArrayList<Integer>();		
		for(int i = 0; i <= elementPosition; i++){
			bufferList.add(pilha.pop());
		}
		for(int i = 0; i <= elementPosition; i++){
			pilha.push(bufferList.get(i));
		}
		saida.printf("%d ", (pilha.size() - elementPosition));		
	}

	private int findMaxElementReturnPosition(Stack<Integer> pilha,
			int stackSize, int compareWithThis) {
		
		Stack<Integer> copia = (Stack<Integer>) pilha.clone();
		
		for(int position = 0; position <= stackSize; position++){
			if(copia.pop().intValue() == compareWithThis){
				return position;
			}
		}		
		return 0; 
	};	
}